package com.t.bricks.bussiness.db.dao.synchtable.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.synchtable.SynchTableDao;
import com.t.bricks.bussiness.db.entity.synchtable.SynchTable;
import com.t.bricks.bussiness.db.entity.synchtable.base.SynchTableBase;
import com.t.bricks.bussiness.db.mapper.synchtable.SynchTableMapper;
import com.t.bricks.bussiness.model.synchtable.ConditionSynchTable;

import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.OrderInfoList;
import com.t.bricks.model.Page;
import com.t.bricks.utils.function.data.IntegerUtil;
import com.t.bricks.utils.function.system.ClassUtil;

@Repository
public class SynchTableDaoImpl implements SynchTableDao {

	@Autowired
	private SynchTableMapper synchTableMapper;

	@Value("${app.findPage.dLikeDate:配置文件中的当前库全文检索时间范围dLikeDate未设置}")
	private String sLikeDate;

	/**
	 * 添加
	 * @param synchTable
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity add(SynchTable synchTable) {
		try {
			int i = synchTableMapper.add(synchTable);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", synchTable);
	}

	/**
	 * 删除
	 * @param uId 记录编号
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity del(String uId) {
		try {
			int i = synchTableMapper.del(uId);
			if (i < 1) {
				Log.error("删除未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "删除数据失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			return new MsgEmity(false, "删除失败！", 7003);
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}

	/**
	 * 修改
	 * 注意:更新成功后对象属性iVersion值将被+1
	 *     当对象的属性值为null时不更新字段
	 * @param synchTable
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity edit(SynchTable synchTable) {
		try {
			int i = synchTableMapper.edit(synchTable);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期大于0):", i);
				return new MsgEmity(false, "修改数据失败！", 7003);
			}
		} catch (Exception e) {
			Log.error("修改操作发生异常:", e.getMessage());
			return new MsgEmity(false, "修改失败！", 7004);
		}
		
		return new MsgEmity(true, "修改成功！", synchTable);
	}
	
	/**
	 * 根据Id查询数据
	 * @param uId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findById(String uId) {
		SynchTable synchTable = null;
		try {
			synchTable = synchTableMapper.findById(uId);
			if (null == synchTable) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", synchTable);
	}

	/**
	 * 根据字段名取指定记录编号的数据库表中对应字段的值
	 * @param uId
	 * @param fieldNames 待取数据的字段名称集合
	 * @return MsgEmity 返回内容data中存放的是Map
	 */
	@Override
	public MsgEmity getValueByFieldName(String uId, List<String> fieldNames) {
		//--检查实体类中是否有该字段,没有就不加入查询字段--//
		Class<?> clazz = SynchTableBase.class;
		List<String> dbNames = new ArrayList<String>();
		for (String field : fieldNames) {
			try {
				if (clazz.getDeclaredField(field) != null) {
					dbNames.add(field);
				}
			} catch (Exception e) {
			}
		}
		
		if (dbNames.size() < 1) {
			return new MsgEmity(false, "没有对应的数据可查询！", 7001);
		}
		
		Map<String, Object> result = null;
		try {
			result = synchTableMapper.getValueByFieldName(uId, dbNames);
			if (null == result) {
				return new MsgEmity(false, "数据不存在！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7003);
		}
		
		//--补充字段--//
		for (String string : fieldNames) {
			if (!result.containsKey(string)) {
				result.put(string, null);
			}
		}
		
		return new MsgEmity(true, "查询成功！", result);
	}

	/**
	 * 根据关键值取对象集合
	 * @param synchTableData 存放查询条件的SynchTable对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByKey(SynchTable synchTableData) {
		List<SynchTable> list = null;
		try {
			list = synchTableMapper.findByKey(synchTableData);
			if (null == list) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
			if (list.size() < 1) {
				return new MsgEmity(true, "查询成功，但没有数据！", list);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

	/**
	 * 根据关键值查数量
	 * @param synchTableData 存放查询条件的SynchTable对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findCountByKey(SynchTable synchTableData) {
		int iCount = 0;
		try {
			iCount = synchTableMapper.findCountByKey(synchTableData);
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7001);
		}
		
		return new MsgEmity(true, "查询成功！", iCount);
	}

	/**
	 * 检查待修改内容是否存在重复数据
	 * @param synchTableData
	 * @return MsgEmity 出现重复则返回true,若码值返回7008则表示发生异常,否则表示没有重复
	 */
	@Override
	public MsgEmity checkRepeatByEdit(SynchTable synchTableData) {
		int i = 0;
		try {
			i = synchTableMapper.checkRepeatByEdit(synchTableData);
			if (0 == i) {
				return new MsgEmity(false, "查询成功！没发现重复", 7007);
			}
		} catch (Exception e) {
			Log.error("检查待修改内容是否存在重复数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败，请查看异常日志！", 7008);
		}
		
		return new MsgEmity(true, "查询成功！发现重复", i);//将查询到的数量(或sql中的构造码)返回以便特殊业务要求
	}

	/**
	 * 检查待新增内容是否存在重复数据
	 * @param synchTableData
	 * @return MsgEmity 出现重复则返回true,若码值返回7008则表示发生异常,否则表示没有重复
	 */
	@Override
	public MsgEmity checkRepeatByAdd(SynchTable synchTableData) {
		int i = 0;
		try {
			i = synchTableMapper.checkRepeatByAdd(synchTableData);
			if (0 == i) {
				return new MsgEmity(false, "查询成功！没发现重复", 7007);
			}
		} catch (Exception e) {
			Log.error("检查待新增内容是否存在重复数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败，请查看异常日志", 7008);
		}
		
		return new MsgEmity(true, "查询成功！发现重复", i);//将查询到的数量(或sql中的构造码)返回以便特殊业务要求
	}
	
	/**
	 * 查询分页信息
	 * @param page 分页信息对象
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPage(Page page, OrderInfoList orders, String sLikeStr,
			ConditionSynchTable condition) {
		if ("配置文件中的当前库全文检索时间范围dLikeDate未设置".equals(sLikeDate)) {
			sLikeDate = "3";
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, - IntegerUtil.strToInt(sLikeDate, 3));//日期加m天
		Date dLikeDateSt = calendar.getTime();
		Date dLikeDateEd = new Date();
		
		int iCount = 0;
		try {
			iCount = synchTableMapper.findByPageCount(sLikeStr, dLikeDateSt, dLikeDateEd, condition);
		} catch (Exception e) {
			Log.error("查询数量发生异常", e.getMessage());
			return new MsgEmity(false, "查询数量失败！", 7001);
		}
		
		if (iCount < 1) {
			return new MsgEmity(true, "没有相关数据！", page);
		}

		page.setCountRow(iCount);

		List<SynchTable> list = null;
		try {
			list = synchTableMapper.findByPage(page,
					(null == orders) ? null : orders.getOrderInfoList(),
					sLikeStr, dLikeDateSt, dLikeDateEd, condition);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}

		page.setRecordList(list);
		
		if (null != sLikeStr) {
			return MsgEmity.success(page, "查询成功！", "由于使用了全文检索,因此数据查询限制为近", sLikeDate, "天创建的数据");
		}
		
		return new MsgEmity(true, "查询成功！", page);
	}
	
	/**
	 * 直接调用数据库操作
	 * @param funName 待调用的mybits函数名
	 * @param argsType 参数类型集合
	 * @param args 参数集合
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity callMapping(String funName, Class<?>[] argsType , Object[] args) {
		if (!ClassUtil.containsMethod(SynchTableMapper.class, funName)) {//先判断是否存在方法
			return new MsgEmity(false, "系统中没有所请求的处理方法，请联系管理员！", 7001);
		}
		
		Object object = null;
		try {
			object = SynchTableMapper.class.getDeclaredMethod(funName, argsType )
					.invoke(synchTableMapper, args);

			if (null == object) {
				Log.error("执行数据库操作'", funName, "'发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "执行失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("执行数据库操作'", funName, "'发生异常:", e.getMessage());
			return new MsgEmity(false, "执行请求失败！", 7003);
		}
		
		return new MsgEmity(true, "执行成功！", object);
	}
	
	/**
	 * 根据记录编号查询符合分页数据的某条记录
	 * @param uId 记录编号
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPageRow(String uId) {
		SynchTable synchTable = null;
		try {
			synchTable = synchTableMapper.findByPageRow(uId);
			if (null == synchTable) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", synchTable);
	}
	
	/**
	 * 查询搜索用的数据
	 * 警告:本方法只允许内部使用,不能开放到外部接口
	 * @param whereStr 查询条件字符串
	 * @param orders 排序数据集合
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findBySearch(String whereStr, OrderInfoList orders) {
		if (null == whereStr || "".equals(whereStr.trim())) {
			return new MsgEmity(false, "查询条件不允许为空！", 7001);
		}

		List<SynchTable> list = null;
		try {
			list = synchTableMapper.findBySearch(whereStr.trim(),
					(null == orders) ? null : orders.getOrderInfoList());
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

	/**
	 * 查询导出到文件的数据
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByExport(OrderInfoList orders, String sLikeStr, ConditionSynchTable condition) {
		if ("配置文件中的当前库全文检索时间范围dLikeDate未设置".equals(sLikeDate)) {
			sLikeDate = "3";
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, - IntegerUtil.strToInt(sLikeDate, 3));//日期加m分
		Date dLikeDateSt = calendar.getTime();
		Date dLikeDateEd = new Date();
		
		List<SynchTable> list = null;
		try {
			list = synchTableMapper.findByExport(
					(null == orders) ? null : orders.getOrderInfoList(),
					sLikeStr, dLikeDateSt, dLikeDateEd, condition);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询导出文件用的数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

	//---------------------------- 自动生成代码结束 ----------------------------//

}
